package com.lishe.lishe.lishe.module.excitation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.bean.DianZanObject;
import com.lishe.lishe.lishe.module.excitation.bean.DianzanImgObject;
import com.lishe.lishe.lishe.module.excitation.interfaces.OnDianZanWallClickListener;
import com.lishe.lishe.lishe.utils.BitmapUtils;
import com.lishe.lishe.lishe.utils.StringUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.lishe.widget.CircleImageView;
import com.lishe.lishe.lishe.widget.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by shino on 2017/10/21.
 */

public class DianZanWallAdapter extends RecyclerviewBaseAdapter {
    private Context context;
    private LinkedList<DianZanObject> dianzanList;
    private ImageLoaderUtils imageLoaderUtils;
    private ImageLoaderUtils imageLoaderUtil2;
    private OnDianZanWallClickListener listener;
    private DisplayImageOptions headOption;
    private int mDisplayWidth;

    public DianZanWallAdapter(Context context, LinkedList<DianZanObject> dianzanList, int displayWidth) {
        this.context = context;
        this.dianzanList = dianzanList;
        imageLoaderUtils = new ImageLoaderUtils();
        imageLoaderUtil2 = new ImageLoaderUtils();
        headOption = imageLoaderUtil2.setOptionsImage(R.mipmap.head);
        this.mDisplayWidth = displayWidth;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_dianzan, null);
        DianZanViewHolder viewHolder = new DianZanViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {//当view没有绑定到screen时，payloads就会失效被丢弃
           onBindViewHolder(holder, position);
        } else {
            updateContent(holder, dianzanList.get(position));
        }
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, final int position) {
        final DianZanObject dianZanObject = dianzanList.get(position);
        if (dianZanObject.getCreateUserType().equals("0")) {
            ((DianZanViewHolder) holder).lable.setVisibility(View.VISIBLE);
        } else {
            ((DianZanViewHolder) holder).lable.setVisibility(View.GONE);
        }
        imageLoaderUtil2.displayImage(dianZanObject.getAvatarPath(), ((DianZanViewHolder) holder).headImg, headOption);
        initIamges(dianZanObject.getImgList(), (DianZanViewHolder) holder);

        updateContent(holder, dianZanObject);
        setListener(holder, position, dianZanObject);
    }

    private void updateContent(RecyclerView.ViewHolder holder, DianZanObject dianZanObject) {
        ((DianZanViewHolder) holder).name.setText(dianZanObject.getEmpName());
        ((DianZanViewHolder) holder).occupation.setText(dianZanObject.getPosName());
        ((DianZanViewHolder) holder).company.setText(dianZanObject.getCreateUserName());
        ((DianZanViewHolder) holder).content.setText(StringUtils.utf8ToString(dianZanObject.getEventDesc()));
        ((DianZanViewHolder) holder).note.setText(dianZanObject.getActionName());
        if (dianZanObject.getShareCount() == 0) {
            ((DianZanViewHolder) holder).forward.setText("转发");
        } else {
            ((DianZanViewHolder) holder).forward.setText(dianZanObject.getShareCount() + "");
        }
        if (dianZanObject.getCommentCount() == 0) {
            ((DianZanViewHolder) holder).comment.setText("评论");
        } else {
            ((DianZanViewHolder) holder).comment.setText(dianZanObject.getCommentCount() + "");
        }
        if (dianZanObject.getGiftCount() == 0) {
            ((DianZanViewHolder) holder).gift.setText("礼物");
        } else {
            ((DianZanViewHolder) holder).gift.setText(dianZanObject.getGiftCount() + "");
        }

        if (dianZanObject.isFlag()) {
            ((DianZanViewHolder) holder).heart.setImageResource(R.mipmap.heart_red);
            ((DianZanViewHolder) holder).dianzan.setTextColor(context.getResources().getColor(R.color.color_FF4138));
        } else {
            ((DianZanViewHolder) holder).heart.setImageResource(R.mipmap.heart_black);
            ((DianZanViewHolder) holder).dianzan.setTextColor(context.getResources().getColor(R.color.color_999999));
        }

        if (dianZanObject.getAdmiredCount() == 0) {
            ((DianZanViewHolder) holder).dianzan.setText("鼓励");
        } else {
            ((DianZanViewHolder) holder).dianzan.setText(dianZanObject.getAdmiredCount() + "");
        }
    }

    private void setListener(RecyclerView.ViewHolder holder, final int position, final DianZanObject dianZanObject) {
        ((DianZanViewHolder) holder).forwardLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickForward(position);
            }
        });
        ((DianZanViewHolder) holder).commentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickComment(position);
            }
        });
        ((DianZanViewHolder) holder).giftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null)
                    listener.onClickGift(position);
            }
        });
        ((DianZanViewHolder) holder).dianzanLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (listener != null) listener.onClickDianZan(position);
            }
        });

        ((DianZanViewHolder) holder).image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dianZanObject.getImgList().size() > 0) {
                    if (listener != null) listener.onClickImage(position, 0);
                }
            }
        });
        ((DianZanViewHolder) holder).image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dianZanObject.getImgList().size() > 1) {
                    if (listener != null) listener.onClickImage(position, 1);
                }
            }
        });
        ((DianZanViewHolder) holder).image3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dianZanObject.getImgList().size() > 2) {
                    if (listener != null) listener.onClickImage(position, 2);
                }
            }
        });
        ((DianZanViewHolder) holder).headImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onClickHead(dianzanList.get(position).getUserId());
            }
        });
        ((DianZanViewHolder) holder).name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onClickHead(dianzanList.get(position).getUserId());
            }
        });
        ((DianZanViewHolder) holder).company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (!dianzanList.get(position).getCreateUserType().equals("0"))
                        listener.onClickHead(dianzanList.get(position).getCreateUserId());
                }
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return dianzanList == null ? 0 : dianzanList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }


    class DianZanViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView headImg;
        private TextView name;
        private TextView occupation;
        private TextView company;
        private TextView content;
        private LinearLayout forwardLayout;
        private TextView forward;
        private LinearLayout giftLayout;
        private TextView gift;
        private LinearLayout commentLayout;
        private TextView comment;
        private LinearLayout dianzanLayout;
        private TextView dianzan;
        private LinearLayout imageLayout;
        private RoundImageView image1;
        private RoundImageView image2;
        private RoundImageView image3;
        private ImageView lable;
        private ImageView heart;
        private TextView note;

        public DianZanViewHolder(View itemView) {
            super(itemView);
            headImg = itemView.findViewById(R.id.head_img);
            name = itemView.findViewById(R.id.name);
            occupation = itemView.findViewById(R.id.occupation);
            company = itemView.findViewById(R.id.company);
            content = itemView.findViewById(R.id.content);
            forwardLayout = itemView.findViewById(R.id.forward_layout);
            forward = itemView.findViewById(R.id.forward);
            giftLayout = itemView.findViewById(R.id.gift_layout);
            gift = itemView.findViewById(R.id.gift);
            commentLayout = itemView.findViewById(R.id.comment_layout);
            comment = itemView.findViewById(R.id.comment);
            dianzanLayout = itemView.findViewById(R.id.dianzan_layout);
            dianzan = itemView.findViewById(R.id.dianzan);
            imageLayout = itemView.findViewById(R.id.image_layout);
            image1 = itemView.findViewById(R.id.image1);
            image2 = itemView.findViewById(R.id.image2);
            image3 = itemView.findViewById(R.id.image3);
            lable = itemView.findViewById(R.id.lable);
            heart = itemView.findViewById(R.id.heart);
            note = itemView.findViewById(R.id.note);

            int width = (mDisplayWidth - Utils.dip2px(context, 40)) / 3;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
            LinearLayout.MarginLayoutParams params1 = params;
            params1.rightMargin = Utils.dip2px(context, 5);
            image1.setLayoutParams(params1);
            image2.setLayoutParams(params1);
            image3.setLayoutParams(params1);
        }
    }

    private void initIamges(LinkedList<DianzanImgObject> imgs, DianZanViewHolder holder) {
        int number = imgs.size();
        Log.e("number", number + "");
        holder.imageLayout.setVisibility(View.VISIBLE);
        switch (number) {
            case 0:
                holder.imageLayout.setVisibility(View.GONE);
                break;
            case 1:
                holder.image1.setVisibility(View.VISIBLE);
                holder.image2.setVisibility(View.INVISIBLE);
                holder.image3.setVisibility(View.INVISIBLE);
                imageLoaderUtils.setImageBitmap(imgs.get(0).getImgUrl(), holder.image1);
                holder.image1.setTag(imgs.get(0).getImgUrl());

                break;
            case 2:
                holder.image1.setVisibility(View.VISIBLE);
                holder.image2.setVisibility(View.VISIBLE);
                holder.image3.setVisibility(View.INVISIBLE);
                holder.image1.setTag(imgs.get(0).getImgUrl());
                holder.image2.setTag(imgs.get(1).getImgUrl());
                imageLoaderUtils.setImageBitmap(imgs.get(0).getImgUrl(), holder.image1);
                imageLoaderUtils.setImageBitmap(imgs.get(1).getImgUrl(), holder.image2);
                break;
            case 3:
                holder.image1.setVisibility(View.VISIBLE);
                holder.image2.setVisibility(View.VISIBLE);
                holder.image3.setVisibility(View.VISIBLE);
                holder.image1.setTag(imgs.get(0).getImgUrl());
                holder.image2.setTag(imgs.get(1).getImgUrl());
                holder.image3.setTag(imgs.get(2).getImgUrl());

                imageLoaderUtils.setImageBitmap(imgs.get(0).getImgUrl(), holder.image1);
                imageLoaderUtils.setImageBitmap(imgs.get(1).getImgUrl(), holder.image2);
                imageLoaderUtils.setImageBitmap(imgs.get(2).getImgUrl(), holder.image3);
                break;
        }

    }

    public void setOnDianZanWallClickListener(OnDianZanWallClickListener listener) {
        this.listener = listener;
    }

    public void unRegisterOnDianZanWallClickListener(OnDianZanWallClickListener listener) {
        this.listener = null;
    }
}
